{extend name="/index/base" /}

{block name="content"}
<div class="layui-card">
    <div class="layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form" >
            <a class="layui-btn layui-btn-sm" href="{:URL('admin/ad/create')}">添 加</a>
            <div class="layui-input-inline">
                <input type="text" name="title" id="title" placeholder="请输入名称" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select name="category_id" lay-verify="required" id="category_id">
                    <option value="">请选择广告位</option>
                    {foreach $positions as $k => $v}
                    <option value="{$v['id']}">{$v['name']}</option>
                    {/foreach}
                </select>
            </div>

            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-sm" id="searchBtn">搜 索</button>            </div>

        </div>
    </div>
    <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
        <script type="text/html" id="options">
            <div class="layui-btn-group">
                {if auth('admin/ad/create')}
                <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                {/if}
                {if auth('admin/ad/destroy')}
                <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                {/if}
            </div>
        </script>
        <script type="text/html" id="thumb">
            <a href="{{d.cover}}" target="_blank" title="点击查看"><img src="{{d.cover}}" alt="" width="28" height="28"></a>
        </script>
        <script type="text/html" id="top">
            {{d.top?'置顶':'未置顶'}}
        </script>
        <script type="text/html" id="status">
            <input type="checkbox" name="status" {{ d.status==1?'checked':''}} lay-filter="status" lay-skin="switch" lay-text="可用|禁用" mid='{{d.id}}'>
        </script>

    </div>
</div>

{/block}


{block name="script"}
<script>
    layui.use(['layer','table','form'],function () {
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.$ //由于layer弹层依赖jQuery，所以可以直接得到
        //用户表格初始化
        var dataTable = table.render({
            elem: '#dataTable'
            ,height: 500
            ,url: "{:URL('admin/ad/getList')}" //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {checkbox: true,fixed: true}
                ,{field: 'id', title: 'ID', sort: true,width:80}
                ,{field: 'name', title: '名称'}
                ,{field: 'content', title: '内容'}
                ,{field: 'sort', title: '排序',sort: true}
                ,{field: 'position_id', title: '广告位'}
                ,{field: 'status', title: '状态', toolbar: '#status'}
                ,{fixed: 'right', width: 260, align:'center', toolbar: '#options'}
            ]]
        });

        //监听工具条
        table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'del'){
                layer.confirm('确认删除吗？', function(index){
                    $.post("{:URL('admin/ad/destroy') }",{ids:[data.id]},function (result) {
                        if (result.code==200){
                            obj.del(); //删除对应行（tr）的DOM结构
                        }
                        layer.close(index);
                        layer.msg(result.msg)
                    });
                });
            } else if(layEvent === 'edit'){
                location.href = '/admin/ad/edit/id/'+data.id;
            }else if(layEvent === 'ad'){
                location.href = '/admin/ad/apply/ad_id/'+data.id;
            }else if (layEvent === 'children'){
                var pid = $("#returnParent").attr("pid");
                if (data.parent_id!=0){
                    $("#returnParent").attr("pid",pid+'_'+data.parent_id);
                }
                dataTable.reload({
                    where:{parent_id:data.id},
                    page:{curr:1}
                })
            }
        });

        //搜索
        $("#searchBtn").click(function () {
            var name = $("#title").val()
            var catId = $("#category_id").val()
            dataTable.reload({
                where:{name:name,'position_id':catId},
                page:{curr:1}
            })
        })

        //返回上一级
        $("#returnParent").click(function () {
            var pid = $(this).attr("pid");
            if (pid!='0'){
                ids = pid.split('_');
                parent_id = ids.pop();
                $(this).attr("pid",ids.join('_'));
            }else {
                parent_id=pid;
            }
            dataTable.reload({
                where:{parent_id:parent_id},
                page:{curr:1}
            })
        })

        //按钮批量操作
        $(".set_top").click(function () {
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length>0){
                $.each(hasCheckData,function (index,element) {
                    ids.push(element.id)
                })
            }
            if (ids.length>0){
                var val = $(this).data('value');
                layer.confirm('确认操作吗？', function(index){
                    $.post("{:URL('admin/ad/setTop') }",{ids:ids,'top':val},function (result) {
                        if (result.code==200){
                            dataTable.reload()
                        }
                        layer.close(index);
                        layer.msg(result.msg)
                    });
                })
            }else {
                layer.msg('请选择数据项')
            }
        })

        form.on('switch(status)', function(obj){
            var id = $(this).attr('mid');
            var value='';
            obj.elem.checked?value='1':value='0';
            var act = 'status';
            $.post("{:URL('admin/ad/updateAct') }",{id:id,'act':act,'value':value},function (result) {
                layer.msg(result.msg)
            });
        });

        //按钮批量操作
        $(".export").click(function () {

            //    var start_time = $("#start_time").val()
            //    var end_time = $("#end_time").val();
            //
            //    location.href = '/admin/index/export/start_time/'+start_time+'/end_time/'+end_time;
            // return false;
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length>0){
                $.each(hasCheckData,function (index,element) {
                    ids.push(element.id)
                })
            }
            if (ids.length>0){

                layer.confirm('确认操作吗？', function(index){
                    location.href = '/admin/ad/export/ids/'+ids;
                    layer.close(index);
                })
            }else {
                layer.msg('请选择数据项')
            }
        })



    })
</script>
{/block}
